<?php
global $smarty;

$thisPage = "emails";

$params = unserialize(stripslashes(getRequestParameter("params")));

$emailsTypes = array();
$emailsTypes[] = array(type => "register_validation", title => "Register validation",   content => getTabContent("register_validation", $smarty));
$emailsTypes[] = array(type => "register_finished",   title => "Register finished",     content => getTabContent("register_finished", $smarty));
$emailsTypes[] = array(type => "remind_sponsoration", title => "Sponsoration reminder", content => getTabContent("remind_sponsoration", $smarty));
$emailsTypes[] = array(type => "profile_update",      title => "Profile update",        content => getTabContent("profile_update", $smarty));
$emailsTypes[] = array(type => "bl_update",           title => "Business lead update",  content => getTabContent("bl_update", $smarty));
$emailsTypes[] = array(type => "ad_expire",           title => "Businesslead expire",   content => getTabContent("ad_expire", $smarty));
$emailsTypes[] = array(type => "forgottenpassword",   title => "Forgotten password",    content => getTabContent("forgottenpassword", $smarty));
$emailsTypes[] = array(type => "usercreated",         title => "User created email",    content => getTabContent("usercreated", $smarty));
$emailsTypes[] = array(type => "invoice",             title => "Payment sender",        content => getTabContent("invoice", $smarty));
$emailsTypes[] = array(type => "sendorder",           title => "Send order",            content => getTabContent("sendorder", $smarty));

addDataService("savecontent");
addDataservice("sendtestemail");

function buildLayout($smarty) {
    global $thisPage;
    global $params;
    global $titles;

    TemplateHelper::useLayout("default", true);
    $smarty->assign("default", array(
        content => getContent($smarty),
        contexthelp => getContextHelp($smarty),
    ));
}

function executeService($serviceName, $params, $smarty) {
    switch ($serviceName) {
        case "savecontent" : { return service_savecontent($params, $smarty); }
        case "sendtestemail" : { return service_sendtestemail($params); }
    }
}

function getTabContent($tabName, $smarty) {
    initDatabase();
    switch ($tabName) {
        case "register_validation" : { return getRegisterValidationTabcontent($smarty); }
        case "register_finished" : { return getRegisterFinishedTabcontent($smarty); }
        case "remind_sponsoration" : { return getSponsorationReminderTabcontent($smarty); }
        case "profile_update" : { return getProfileUpdateTabcontent($smarty); }
        case "bl_update" : { return getBlUpdateTabcontent($smarty); }
        case "ad_expire" : { return getAdExpireTabcontent($smarty); }
        case "forgottenpassword" : { return getForgottenPasswordTabcontent($smarty); }
        case "usercreated" : { return getUserCreatedTabcontent($smarty); }
        case "invoice" : { return getInvoiceTabcontent($smarty); }
        case "sendorder" : { return getSendOrdertabContent($smarty); }
    }
}

function service_sendtestemail($params) {
    $templates = array(
        firstname => "Test",
        lastname => "User",
        title => "Dr.",
        companyname => "Test company",
        link => "test link",
        username => "testuser",
        password => "testpassword",
    );
    sendEmail($params['emailname'], $params['email'], $templates);
    return serviceSuccessful(array());
}

function getRegisterValidationTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("register_validation");
    $smarty->assign("registervalidation", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.register_validation", true));
}

function getForgottenPasswordTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("forgottenpassword");
    $smarty->assign("forgottenpassword", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.forgottenpassword", true));
}

function getSendOrderTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("sendorder");
    $smarty->assign("sendorder", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.sendorder", true));
}

function getRegisterFinishedTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("register_finished");
    $smarty->assign("registerfinished", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.register_finished", true));
}

function getAdExpireTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("ad_expire");
    $smarty->assign("adexpire", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.ad_expire", true));
}

function getInvoiceTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("invoice");
    $smarty->assign("invoice", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.invoice", true));
}

function getSponsorationReminderTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("sponsor_reminder");
    $smarty->assign("sponsorreminder", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.sponsor_reminder", true));
}

function getUserCreatedTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("usercreated");
    $smarty->assign("usercreated", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.usercreated", true));
}

function getProfileUpdateTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("profile_update");
    $smarty->assign("profileupdate", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.profile_update", true));
}

function getBlUpdateTabContent($smarty) {
    global $thisPage;

    Database::loadModel("email");
    $content = Database::getItem("bl_update");
    $smarty->assign("blupdate", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.bl_update", true));
}

function getContent($smarty) {
    global $thisPage;
    global $emailsTypes;
    
    $smarty->assign("emailstabtypes", $emailsTypes);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "content", true));
}

function getContextHelp($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "contexthelp", true));
}

function service_savecontent($params, $smarty) {
    Database::loadModel("email");
    Database::getModel()->saveEmailFromParams("register_validation", $params);
    Database::getModel()->saveEmailFromParams("register_finished", $params);
    Database::getModel()->saveEmailFromParams("sponsor_reminder", $params);
    Database::getModel()->saveEmailFromParams("profile_update", $params);
    Database::getModel()->saveEmailFromParams("ad_expire", $params);
    Database::getModel()->saveEmailFromParams("forgottenpassword", $params);
    Database::getModel()->saveEmailFromParams("usercreated", $params);
    Database::getModel()->saveEmailFromParams("invoice", $params);
    Database::getModel()->saveEmailFromParams("sendorder", $params);
    return serviceSuccessful(array(
    ));
}


function getPageHeader() {
}

function getPageFooter() {
}

?>
